var api = require("../../api/api.js");
var storageManage = require("../../manage/storageManage.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
Component({
  options: {
    multipleSlots: false // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    derived: String,
    taptype: String
  },
  data: {
    // 选择参数弹出框
    animationData: "",
    showModalStatus: false,
    amount: 0,
    windowHeight: ''
  },
  methods: {
    //展示弹框
    showModalContainer(data) {
      var that = this;
      
      wx.getSystemInfo({
        success: function (res) {
          that.data.windowHeight = res.windowHeight;
        }
      });
      var animation = wx.createAnimation({
        duration: 50,
        timingFunction: "linear",
        delay: 0
      })
      animation.translateY(this.data.windowHeight).step();
      this.setData({
        mCommodityId: data.data.commodity_id,
        commodity_name: data.data.commodity_name,
        small_image: data.data.small_image,
        price: data.data.price,
        skus: data.data.skus,
        animationData: animation.export(),
        showModalStatus: true
      });

      setTimeout(function () {
        animation.translateY(0).step();
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 200)

    },
    // 隐藏遮罩层
    hideModal: function () {
      var that=this;
      wx.getSystemInfo({
        success: function (res) {
          that.data.windowHeight = res.windowHeight;
        }
      });
      var animation = wx.createAnimation({
        duration: 50,
        timingFunction: "linear",
        delay: 0
      })
      this.animation = animation;
      animation.translateY(this.data.windowHeight).step()
      this.setData({
        animationData: animation.export(),
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false
        })
      }.bind(this), 200);
      this.triggerEvent('hideModal')
    },
    //立即购买
    onBuyClick: utils.throttle(function () {
      let that = this;
      let params = [];
      var amountType = false;
      let skus = this.data.skus; //规格数据
      for (let i = 0; i < skus.length; i++) {
        for (let j = 0; j < skus[i].sku.length; j++) {
          if (skus[i].sku[j].amount > 0) {
            amountType = true;
          }
        }
      };
      if (!amountType) {
        wx.showToast({
          title: '请至少添加一件商品',
          icon: 'none'
        });
        return
      }
     
      for (let i = 0; i < skus.length; i++) {
        for (let j = 0; j < skus[i].sku.length; j++) {
          if (skus[i].sku[j].amount > 0) {
            params.push({
              "sku_id": skus[i].sku[j].skuId.toString(),
              "amount": skus[i].sku[j].amount,
              'commodity_id': that.data.mCommodityId
            });
          }
        }
      };
     
      wx.navigateTo({
        url: '../payinfo/payinfo?params=' + JSON.stringify(params),
      });
    }),
    /**
 * 加入购物车
 */
    addcart: utils.throttle(function () {
      var that = this;
      var skus = this.data.skus; //规格数据
      var amountType=false;
      for (let i = 0; i < skus.length; i++) {
        for (let j = 0; j < skus[i].sku.length; j++) {
          if (skus[i].sku[j].amount > 0) {
            amountType = true;
          }
        }
      };
      if (!amountType){
       wx.showToast({
         title: '请至少添加一件商品',
         icon: 'none'
       });
       return
     }
      
      let params = [];
      for (let i = 0; i < skus.length; i++) {
        for (let j = 0; j < skus[i].sku.length; j++) {
          if (skus[i].sku[j].amount > 0) {
            params.push({
              "sku_id": skus[i].sku[j].skuId.toString(),
              "amount": skus[i].sku[j].amount,
            });
          }
        }
      };
      api.default.api('add_cart', {
        "skus": params
      }, function (data) {
        if (data.e == 0) {
          that.addcartResult();
        }
      }, function (e) {

      });

    }),

    addcartResult: function () {
      var that = this;
      that.setData({
        showModalStatus: false
      })
      setTimeout(function(){
        wx.showToast({
          title: '添加成功！',
          icon: 'none'
        });
      },300)
      event.notify('cart', '');
    },

    numChange: function (e) {

      let skus = this.data.skus;
      let attrIndex = e.currentTarget.dataset.attrindex;
      let index = e.currentTarget.dataset.index;
      let num = skus[attrIndex].sku[index].amount;
      var status = e.currentTarget.dataset.status;
      var item_num = "skus[" + attrIndex + "].sku[" + index + "].amount";
      if (status == 1) {
        num += 1;
      } else {
        num <= 0 ? 0 : num -= 1;
      }


      this.setData({
        [item_num]: num,
      });
      this.count()
    },
    count: function () {
      let amount = 0;
      let skus = this.data.skus;
      for (let i = 0; i < skus.length; i++) {
        let item = skus[i].sku;
        for (let j = 0; j < item.length; j++) {
          let itemSku = item[j]
          amount += ( itemSku.amount * (itemSku.price * 100)) / 100
        }
      }
      this.setData({
        amount: amount
      })
    }
  }
})
